package net.telesing.tcmc.back.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.telesing.tcmc.back.logic.DataLogic;
import net.telesing.tcmc.back.model.DataModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.back.modelqp.DataQp;
import net.telesing.tcmc.common.Constants;
import net.telesing.tcmc.common.QueryReturn;
import net.telesing.tcmc.core.web.AjaxBean;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

/**
 * 资料说明Action
 *
 * Created by Zmt on 2015/11/10.
 */
@At("/data")
@IocBean
public class DataAction
{
    private static final Logger log = LogManager.getLogger(DataAction.class);

    @Inject
    private DataLogic dataLogic;

    /**
     * 跳转到“资料说明”页面
     */
    @At
    @Ok("jsp:${obj.jsp}")
    public HashMap<String, Object> toPage(@Param("..") DataQp dataQp, @Param("isSearch") boolean isSearch, @Attr(Constants.SESSION_KEY) final UserModel op)
    {
        log.info("DataAction --> toPage");
        HashMap<String, Object> map = new HashMap<String, Object>();
        dataQp = dataLogic.getDataQp(dataQp,op);
        map.put("queryReturn", dataLogic.queryData(dataQp));
        map.put("this_page", dataQp.getPage());
        map.put("jsp", dataLogic.getJsp(op, isSearch));
        map.put("typeDrop", dataLogic.getDataType());
        return map;
    }

    @At
    @Ok("json")
    public QueryReturn queryData(@Param("..") DataQp dataQp)
    {
        log.info("DataAction --> queryData");
        return dataLogic.queryData(dataQp);
    }

    @At
    @Ok("json")
    public AjaxBean create(@Param("..") DataModel dataModel, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("DataAction --> create");
        return dataLogic.create(dataModel, op.getId());
    }

    @At
    @Ok("json")
    public AjaxBean modify(@Param("..") DataModel dataModel, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("DataAction --> modify");
        return dataLogic.modify(dataModel, op.getId());
    }

    /**
     * 批量删除文件
     *
     * @param ids
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean delete(@Param("ids") long[] ids)
    {
        log.info("DataAction --> delete");
        return dataLogic.delete(ids);
    }

    /**
     * 删除文件
     *
     * @param id
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean deleteData(@Param("id") long id)
    {
        log.info("DataAction --> delete");
        return dataLogic.delete(id);
    }

    @At
    @Ok("json")
    @AdaptBy(type = UploadAdaptor.class)
    public AjaxBean uploadFile(@Param("fileToUpload") TempFile tempFile)
    {
        log.info("DataAction --> uploadFile");
        return dataLogic.uploadFile(tempFile);
    }

    @At
    @Ok("raw")
    public File download(HttpServletResponse response, @Param("id") long id) throws Exception
    {
        return dataLogic.download(response, id);
    }

    /**
     * 根据ID获取文件
     *
     * @param id
     * @return
     */
    @At
    @Ok("jsp:pages.back.data.data_dialog")
    public Map<String, Object> fetchData(@Param("id") long id)
    {
        log.info("dataAction --> fetchData");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", dataLogic.fetchData(id));
        map.put("typeDrop", dataLogic.getDataType());
        map.put("isModify", true);
        return map;
    }

}
